﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class exchange_manage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USERNAME"] == null)
        {
            Response.Redirect("error_log.aspx", true);
        }
        if (Session["IDENTITY"].Equals("employee") == true)
        {
            Response.Redirect("error_auth.aspx", true);
        }
        string name = Session["USERNAME"].ToString();
        SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc.Open();
        SqlCommand com_get = new SqlCommand();
        com_get.Connection = sc;
        com_get.CommandText = "select INFO_PRODUCT.PRODUCT_ID,PRODUCT_NAME,CREDITS_RATE,PRODUCT_SORT,PRODUCT_INTRO,PRODUCT_IMAGE,INFO_EXCHANGE.EX_SUM from INFO_PRODUCT,INFO_EXCHANGE where INFO_EXCHANGE.PRODUCT_ID=INFO_PRODUCT.PRODUCT_ID and INFO_PRODUCT.PRODUCT_ID=(select INFO_EXCHANGE.PRODUCT_ID from INFO_EXCHANGE,INFO_PRODUCT where INFO_PRODUCT.ENTER_ID=@eid1 and INFO_EXCHANGE.PRODUCT_ID=INFO_PRODUCT.PRODUCT_ID)";
        com_get.Parameters.AddWithValue("@eid1", Session["ENTERID"].ToString());
        SqlDataReader reader = com_get.ExecuteReader();
        GridView1.DataSource = reader;
        GridView1.DataBind();
        sc.Close();
    }
    protected void delete(object sender, GridViewDeleteEventArgs e)
    {

        String id = GridView1.Rows[e.RowIndex].Cells[0].Text;
        SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc1.Open();
        SqlCommand del = new SqlCommand();
        del.Connection = sc1;
        del.CommandText = "delete from INFO_EXCHANGE where PRODUCT_ID=@pid";
        del.Parameters.AddWithValue("@pid", id);
        while (Application["sqllock"] == "lock")
        {//检测是否已被锁住
            System.Threading.Thread.Sleep(2000);
        }
        Application["sqllock"] = "lock";//上锁           
        if (del.ExecuteNonQuery() == 1)
        { 
            Application["sqllock"] = "unlock";//解锁
            sc1.Close();
            Page.RegisterStartupScript("ss", "<script>alert('撤销换购商品成功！')</script>");
            return;
        }
        else
        {
            Application["sqllock"] = "unlock";//解锁
            sc1.Close();
            Page.RegisterStartupScript("ss", "<script>alert('数据库异常，请重试！')</script>");
            return;
        }
    }
}